package com.lew.scott.happy.birthday;

import java.util.Date;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.lew.scott.date.LunarDate;
import com.lew.scott.date.Solar2Lunar;

public class HappyBirthdayActivity extends Activity {

	private static final String PRESS_BACK_KEY_HINT = "再按一次退出";
	private static final int PRESS_BACK_KEY_WAIT_TIME = 1500;

	private long lastPressBackKeyTime;
	private WakeLock wakeLock = null;

	private HappyBirthdayView happyBirthdayView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		// 保持屏幕常亮
		PowerManager powerManager = (PowerManager) this.getSystemService(Activity.POWER_SERVICE);
		wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		try {
			// 解读生日日期
			LunarDate todayLunarDate = Solar2Lunar.convertDate2LunarDate(new Date());
			String dateStr = getResources().getString(R.string.birthday_lunar);
			String[] sa = dateStr.split("-");
			int month = Integer.parseInt(sa[0]), day = Integer.parseInt(sa[1]);
			int cmp = todayLunarDate.compareTo(month, day);
			if (cmp != 0) {
				setContentView(R.layout.main);
				TextView textView = (TextView) this.findViewById(R.id.textView);
				if (cmp < 0) {
					textView.setText("你的生日是\"" + LunarDate.toString(month, day) + "\"\n" +
							"今天是\"" + todayLunarDate.toString() + "\"\n" +
							"还没到呢，等到" + LunarDate.toString(month, day) + "再打开看看吧"
							);
				}
				if (cmp > 0) {
					textView.setText("你的生日是\"" + LunarDate.toString(month, day) + "\"\n" +
							"今天是\"" + todayLunarDate.toString() + "\"\n" +
							"已经过去了，来年" + LunarDate.toString(month, day) + "再打开看看吧"
							);
				}
				return;
			}
		} catch (Exception e) {
		}

		happyBirthdayView = new HappyBirthdayView(this);
		setContentView(happyBirthdayView, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
	}
	@Override
	protected void onResume() {
		super.onResume();
		wakeLock.acquire();
	}

	@Override
	protected void onPause() {
		super.onPause();
		wakeLock.release();
	}

	/**
	 * 创建options菜单
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// 充实菜单
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);
		// 注意：必须要调用超类的方法，否则无法实现意图回调
		return (super.onCreateOptionsMenu(menu));
	}

	/**
	 * options菜单相应函数
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.about : {
				showAbout();
				break;
			}
			case R.id.exit : {
				break;
			}
		}
		return (super.onOptionsItemSelected(item));
	}

	/**
	 * 实现再按一次退出
	 */
	public void onBackPressed() {
		long currentTime = System.currentTimeMillis();
		if ((currentTime - lastPressBackKeyTime) >= PRESS_BACK_KEY_WAIT_TIME) {
			Toast.makeText(this, PRESS_BACK_KEY_HINT, Toast.LENGTH_SHORT).show();
			lastPressBackKeyTime = currentTime;
		} else {
			finish();
		}
	}

	/**
	 * 显示关于信息
	 */
	private void showAbout() {
		final Dialog aboutDlg = new Dialog(this);
		LayoutInflater inflater = aboutDlg.getLayoutInflater();
		View aboutView = inflater.inflate(R.layout.about_view, null);
		aboutDlg.setContentView(aboutView);
		aboutDlg.setTitle("生日快乐");

		aboutView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				aboutDlg.dismiss();
			}
		});
		aboutDlg.show();
	}
}